package com.bookstore.controller;

import com.bookstore.bookfactory.BeanFactory;
import com.bookstore.action.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

/**
 * Created by Admin on 2016/8/12.
 */
@WebServlet("/")
public class DispatcherServlet extends HttpServlet {
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String uri = request.getRequestURI();
        System.out.println(uri);
        String splits[] = uri.substring(1).split("/");

        String actionName = splits[0];
        String methodName = splits[1];
        System.out.println(actionName);
        System.out.println(methodName);
        // 得到具体类
        try {
            Class clazz = Class.forName("com.bookstore.action." + actionName.substring(0, 1).toUpperCase() + actionName.substring(1) + "Action");
//            Object action = clazz.newInstance();
            Object action = BeanFactory.getBean(actionName + "Action");
            Method method = clazz.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            ModelAndView mv = (ModelAndView) method.invoke(action, request, response);
            if (mv == null) {
                return;
            }
            String view = mv.view;
            if (mv.redirect == ModelAndView.REDIRECT) {
                response.sendRedirect(view + ".jsp");
                return;
            }
           /*
           text
           StringBuffer sb = new StringBuffer();
            for (String key : data.keySet()) {
                sb.append("key:" + key + "\tvalue:" + data.get(key)+"\n");
            }
            response.setCharacterEncoding("utf-8");
            response.setContentType("text/plain");
            response.getWriter().write(sb.toString());*/
             /*

            response.setCharacterEncoding("utf-8");
            response.setContentType("text/plain");
            response.getWriter().write(sb.toString());*/


         /*
          json
          response.setCharacterEncoding("utf-8");
            response.setContentType("application/json");
            Gson gson = new Gson();
            response.getWriter().write(gson.toJson(mv.data));*/

// freemarker
           /*

            jsp
            */
            Map<String, Object> data = mv.data;
            for (String key : data.keySet()) {
                request.setAttribute(key, data.get(key));
            }
            request.getRequestDispatcher(view + ".jsp").forward(request, response);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            Throwable t = e.getCause();
            t.printStackTrace();
            throw new RuntimeException();
        }
    }
}
